package ch1.syn;

/**
 * 锁对象用i，i是不可预判的，出现了多个线程同时访问资源代码
 * 锁对象用object，i是2~6，因为同一个锁对象，线程有序访问资源代码
 */
public class TestIntegerSyn {
    public static void main(String[] args) {
        Worker worker = new Worker(1);
        for (int i = 0; i < 5; i++) {
            new Thread(worker).start();
        }
    }

    private static class Worker implements Runnable {
        private Integer i;
        private Object object = new Object();

        public Worker(Integer i) {
            this.i = i;
        }

        @Override
        public void run() {
            synchronized (i) {
                Thread thread = Thread.currentThread();
                i++;
                System.out.println(thread.getName() + "————" + i);
            }
        }
    }
}
